/**微信，对接后台数据等相关接口 */
var GWx = {
    /**登录微信 */
    login(){
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            wx.login({
                success(res){
                    console.log("微信平台成功登录", res);
                    GWx.register(res);
                },
                fail(){
                    console.error("微信平台登录失败!");
                },
            });
        }else{
            //不是微信环境
           /*  window.switcher = 0;//分享开关关闭
            GEvent.dispatcher("loadGameConfig");
            GEvent.dispatcher("loadPlayerData"); */
        }
    }, 

    /**用户注册获取或刷新token */
    register(res){
        var url =  GConstant.HOST + "register";
        var data = {
            code: res.code,
            version: 1,
            refresh: false,
        };
        var callBack = function(res){
            console.log("register res", res);
            if(res.code === 0){//登录成功
                //获取token
                GHttp.token = res.data.token;
                //获取开关状态
                GWx.switcher();
                //获取玩家数据
                GPlayerData.loadPlayerData();
				//获取openid并上报
                GHttp.openId = res.data.openid;
                AladinSDK.report(AladinSDK.ReportTypes.OPEN_ID, GHttp.openId);
                if(res.data.regist){
                    AladinSDK.ReportAnalytics("wx4233cc143076bfdc",4);
                }
                //点击进入上报
                GWx.sendClick();
                //用户注册
                GReport.progress(2);
                //获取玩家IP所在的城市
                GPlayerData.loadCity();
            }
            else{
                console.error("获取token失败!!!");
            }
        };
        GHttp.Post(url, data, callBack);
    },

    /**获取开关状态  switcher > 0 可以分享 */
    switcher(){
        var url = GConstant.HOST + "switch";
        var data = {
            version: GConstant.version
        };
        var callBack = function(data){
            console.log("switcher:", data);
            if(data.code === 0){
                GConstant.switcher = data.data.status;
            }
        };
        GHttp.Post(url, data, callBack);
    },

    /**点击进入上报 */
    sendClick(){
        var cover = wx.getLaunchOptionsSync().query.cover;//图片id
        var scene = wx.getLaunchOptionsSync().query.scene;//场景id
        if(cover && scene){
            GReport.sendClick(2, Number(cover), Number(scene));
            GReport.reportScene( Number(scene),3);
        }
    },

};

window.GWx = GWx;